package com.itheima.service.impl;

import com.itheima.Vo.BusinessReportVo;
import com.itheima.Vo.MemberReportVo;
import com.itheima.Vo.SetmealReportVo;
import com.itheima.dao.ReportDao;
import com.itheima.entity.HostSetmeal;
import com.itheima.entity.MemberReportSelectVo;
import com.itheima.entity.SetmealCount;
import com.itheima.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportDao reportDao;

    /**
     * 获取会员数量统计数据
     * @return
     */
    @Override
    public MemberReportVo getMemberReport() {
        //根据数据查询对应的月份最后一天和该月份注册人数
        List<MemberReportSelectVo> memberReportSelectVos = reportDao.getMemberReport();

        MemberReportVo memberReportVo = new MemberReportVo();
        List<LocalDate> months = new ArrayList<>();
        List<Integer> memberCount = new ArrayList<>();
        for (MemberReportSelectVo memberReportSelectVo : memberReportSelectVos) {
            months.add(memberReportSelectVo.getMonths());
            memberCount.add(memberReportSelectVo.getMemberCount());
        }
        memberReportVo.setMonths(months);
        memberReportVo.setMemberCount(memberCount);
        return memberReportVo;
    }

    @Override
    public SetmealReportVo getSetmealReport() {
        //先获取预约表中所有的套餐id/名
        List<String> setmealNames = reportDao.getSetmealNames();

        //根据套餐id/名查询对应数量
        List<SetmealCount> setmealCount = reportDao.getSetmealCount();

        SetmealReportVo setmealReportVo = new SetmealReportVo();
        setmealReportVo.setSetmealNames(setmealNames);
        setmealReportVo.setSetmealCount(setmealCount);
        return setmealReportVo;


    }

    /**
     * 获取运营统计数据
     * @return
     */
    @Override
    public BusinessReportVo getBusinessReportData() {
        BusinessReportVo businessReportVo = new BusinessReportVo();
        businessReportVo.setReportDate(LocalDate.now());
        //获取本日、本周、本月新增会员数
        BusinessReportVo businessReportVo1 = reportDao.getMemberCountByDate(businessReportVo.getReportDate());
        businessReportVo.setTodayNewMember(businessReportVo1.getTodayNewMember());
        businessReportVo.setThisWeekNewMember(businessReportVo1.getThisWeekNewMember());
        businessReportVo.setThisMonthNewMember(businessReportVo1.getThisMonthNewMember());
        businessReportVo.setTotalMember(businessReportVo1.getTotalMember());

        //获取本日本周本月预约数到诊数
        BusinessReportVo businessReportVo2 = reportDao.getOrderCountByDate(businessReportVo.getReportDate());
        businessReportVo.setTodayOrderNumber(businessReportVo2.getTodayOrderNumber());
        businessReportVo.setThisWeekOrderNumber(businessReportVo2.getThisWeekOrderNumber());
        businessReportVo.setThisMonthOrderNumber(businessReportVo2.getThisMonthOrderNumber());
        businessReportVo.setTodayVisitsNumber(businessReportVo2.getTodayVisitsNumber());
        businessReportVo.setThisWeekVisitsNumber(businessReportVo2.getThisWeekVisitsNumber());
        businessReportVo.setThisMonthVisitsNumber(businessReportVo2.getThisMonthVisitsNumber());

        //获取热门套餐
        List<SetmealCount> setmealCounts = reportDao.getSetmealCount();
        int totalCount = 0;
        for (SetmealCount setmealCount : setmealCounts) {
            totalCount += setmealCount.getValue();
        }
        List<HostSetmeal> hotSetmeal = new ArrayList<>();
        for (SetmealCount setmealCount : setmealCounts) {
            BigDecimal proportion = new BigDecimal(setmealCount.getValue()).divide(new BigDecimal(totalCount),3, RoundingMode.HALF_UP);
            HostSetmeal newHostSetmeal = new HostSetmeal();
            newHostSetmeal.setProportion(proportion);
            newHostSetmeal.setName(setmealCount.getName());
            newHostSetmeal.setSetmeal_count(setmealCount.getValue());
            hotSetmeal.add(newHostSetmeal);
        }
        businessReportVo.setHotSetmeal(hotSetmeal);
        return businessReportVo;
    }

    /**
     * 导出运营数据报表
     * @param response
     */
    @Override
    public void exportBusinessReport(HttpServletResponse response) throws IOException {
        //查询概览数据
        BusinessReportVo businessReportVo = getBusinessReportData();
        //2. 通过POI将数据写入到Excel文件中
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("templates/report_template.xlsx");
        //基于模板文件创建一个新的Excel文件
        XSSFWorkbook excel = new XSSFWorkbook(in);
        //获取表格文件的Sheet页
        XSSFSheet sheet = excel.getSheet("Sheet1");
        //填充数据--日期
        sheet.getRow(2).getCell(5).setCellValue(LocalDate.now().toString());
        //获得第5行
        XSSFRow row = sheet.getRow(4);
        row.getCell(5).setCellValue(businessReportVo.getTodayNewMember());//新增会员数
        row.getCell(7).setCellValue(businessReportVo.getTotalMember());//总会员数
        //获得第6行
        row = sheet.getRow(5);
        row.getCell(5).setCellValue(businessReportVo.getThisWeekNewMember());//本周新增会员数
        row.getCell(7).setCellValue(businessReportVo.getThisMonthNewMember());//本月新增会员数
        //获得第8行
        row = sheet.getRow(7);
        row.getCell(5).setCellValue(businessReportVo.getTodayOrderNumber());//今日预约数
        row.getCell(7).setCellValue(businessReportVo.getTodayVisitsNumber());//今日到诊数
        //获得第9行
        row = sheet.getRow(8);
        row.getCell(5).setCellValue(businessReportVo.getThisWeekOrderNumber());//本周预约数
        row.getCell(7).setCellValue(businessReportVo.getThisWeekVisitsNumber());//本周到诊数
        //获得第10行
        row = sheet.getRow(9);
        row.getCell(5).setCellValue(businessReportVo.getThisMonthOrderNumber());//本月预约数
        row.getCell(7).setCellValue(businessReportVo.getThisMonthVisitsNumber());//本月到诊数
        //热门套餐
        for (int i = 0;i < businessReportVo.getHotSetmeal().size();i++){
            row = sheet.getRow(12 + i);
            row.getCell(4).setCellValue(businessReportVo.getHotSetmeal().get(i).getName());//套餐名称
            row.getCell(5).setCellValue(businessReportVo.getHotSetmeal().get(i).getSetmeal_count());//预约数量
            row.getCell(6).setCellValue(businessReportVo.getHotSetmeal().get(i).getProportion().toString());//占比
        }


        //3. 通过输出流将Excel文件下载到客户端浏览器
        ServletOutputStream out = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");//指定文件类型
        excel.write(out);

        //关闭资源
        out.close();
        excel.close();

    }

}
